<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="botton-list">
          <div class="button-wrapper">
          <!--使用state对象方式的映射  -->
            <div class="button"> {{this.currentCity}}</div>
          </div>
        </div>
      </div>

      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="botton-list">
          <div class="button-wrapper" v-for="item in hot" :key="item.id"
          @click="handleCityClick(item.name)">
            <div class="button">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <!--  对对象数组进行两次循环  对象是可以进行循环的-->
      <div class="area"
       v-for="(item, key) in cities"
      :key="key" :ref="key">
      <!--  设置一个key的dom节点 -->
        <div class="title border-topbottom">{{ key }}</div>
        <!--  对key值下面的数据循环-->
        <div class="item-list"
        v-for="innerItem in item"
        :key="innerItem.id"
        @click="handleCityClick(innerItem.name)">
          <div class="item border-bottom">{{ innerItem.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  methods: {
    ...mapMutations(['localCity']),
    // 改变当前城市
    handleCityClick (city) {
      // 向vuex的actions触发事件
      //                    函数名        参数
      // this.$store.dispatch('changeCity', city)
      // 直接通过commit调取mutations改变数据
      // this.$store.commit('localCity', city)
      this.localCity(city)
      this.$router.push('/')
    }
  },
  computed: {
    ...mapState({
      // 对象方式的vuex映射
      currentCity: 'city'
    })
  },
  // 接受父组件传递的值
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  watch: {
    // 监听letter的变化,当值发生改变就触发事件
    letter () {
      // 当letter 不为空
      if (this.letter) {
        // 滚动区自动滚动到该区域
        const element = this.$refs[this.letter][0]
        // scrollToElement 必须接收dom节点
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    // dom 挂载完毕执行
    this.scroll = new BScroll(this.$refs.wrapper)
    // 找到文档中的所需节点
    // let wrapper = document.querySelector('.wrapper')
    // 在实例BScroll类
    // lthis.scroll = new BScroll(wrapper)
  }
}
</script>

<style lang="stylus" scoped>
.border-topbottom
  &:before
    border-color: #ccc
  &:after
    border-color: #ccc
.border-bottom
  &:before
    border-color: #ccc
.list
  overflow: hidden
  position: absolute
  top: 1.58rem
  left: 0
  right: 0
  bottom: 0
  .title
    line-height: .54rem
    background: #eee
    padding-left: .2rem
    font-size: .26rem
    color: #666
  .botton-list
    padding: .1rem .6rem .1rem .1rem
    overflow: hidden
    .button-wrapper
      float: left
      width: 33.33%
      .button
       margin: .1rem
       padding: .1rem 0
       text-align: center
       border: .02rem solid #ccc
       border-radius: .06rem
  .item-list
    .item
      line-height: .76rem
      padding-left: .2rem
</style>
